﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GeneralDashboard.Controllers
{
    /// <summary>
    /// Movable.xaml 的交互逻辑
    /// </summary>
    public partial class Movable : UserControl
    {
        private bool IsDragging { get; set; }
        private bool IsStretching { get; set; }
        private double X { get; set; }
        private double Y { get; set; }

        public Movable()
        {
            InitializeComponent();
        }

        private void UserControl_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                if (IsDragging)
                {
                    Drag(sender);
                }
                if (IsStretching)
                {
                    Stretch(sender);
                }
            }
        }

        private void border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
        }

        private void border_MouseLeave(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
                return;

            Border border = (Border)sender;
            var rightLimit = border.ActualWidth - border.Padding.Right;
            var bottomLimit = border.ActualHeight - border.Padding.Bottom;

            var x = Mouse.GetPosition((IInputElement)sender).X;
            var y = Mouse.GetPosition((IInputElement)sender).Y;
            bool stretchRight = (x >= rightLimit && x < border.ActualWidth) ? true : false;
            bool stretchBottom = (y >= bottomLimit && y < border.ActualHeight) ? true : false;

            X = x;
            Y = y;
            IsStretching = true;

            if (stretchRight && stretchBottom)
            {
                Cursor = Cursors.SizeNWSE;
                return;
            }
            else if (stretchRight && !stretchBottom)
            {
                Cursor = Cursors.SizeNS;
                return;
            }
            else
            {
                Cursor = Cursors.Arrow;
                IsStretching = false;
            }
        }

        private void border_MouseEnter(object sender, MouseEventArgs e)
        {
            Border border = (Border)sender;

            var rightLimit = border.ActualWidth - border.Padding.Right;
            var bottomLimit = border.ActualHeight - border.Padding.Bottom;

            var x = Mouse.GetPosition((IInputElement)sender).X;
            var y = Mouse.GetPosition((IInputElement)sender).Y;

            if (x < rightLimit && y < bottomLimit)
                Cursor = Cursors.Arrow;
        }

        private void Drag(object sender)
        {
            Cursor = Cursors.Hand;
            var newX = Mouse.GetPosition((IInputElement)sender).X;
            var newY = Mouse.GetPosition((IInputElement)sender).Y;
            var transformGroup = RenderTransform as TransformGroup;
            if (transformGroup == null)
                return;

            var translateTransforms = from transform in transformGroup.Children
                                      where transform.GetType().Name == "TranslateTransform"
                                      select transform;
            foreach (TranslateTransform tt in translateTransforms)
            {
                tt.X += newX - X;
                tt.Y += newY - Y;
            }

            X = newX;
            Y = newY;
        }

        private void Stretch(object sender)
        {
            var mousePosX = Mouse.GetPosition((IInputElement)sender).X;
            var mousePosY = Mouse.GetPosition((IInputElement)sender).Y;

            Border border = (Border)(IInputElement)sender;
            var xDiff = mousePosX - X;
            var yDiff = mousePosY - Y;
            var width = ((Border)(IInputElement)sender).Width;
            var heigth = ((Border)(IInputElement)sender).Height;


            //make sure not to resize to negative width or heigth
            xDiff = (border.Width + xDiff) > border.MinWidth ? xDiff : border.MinWidth;
            yDiff = (border.Height + yDiff) > border.MinHeight ? yDiff : border.MinHeight;


            // stretchRight && stretchBottom ?
            if (Cursor == Cursors.SizeNWSE)
            {
                ((Border)(IInputElement)sender).Width += xDiff;
                ((Border)(IInputElement)sender).Height += yDiff;
            }
            // stretchRight ? 
            else if (Cursor == Cursors.SizeWE)
                ((Border)(IInputElement)sender).Width += xDiff;

            // stretchBottom ?
            else if (Cursor == Cursors.SizeNS)
                ((Border)(IInputElement)sender).Height += yDiff;

            //no stretch
            else
            {
                Cursor = Cursors.Arrow;
                IsStretching = false;
            }

            // update current coordinates with the latest postion of the mouse
            X = mousePosX;
            Y = mousePosY;
        }
    }
}
